package 数据结构和算法入门计划8月开始;

import java.util.HashSet;
import java.util.Set;
// https://leetcode-cn.com/problems/linked-list-cycle/
public class 环形链表1410821 {

      class ListNode {
          int val;
          ListNode next;
          ListNode(int x) {
              val = x;
              next = null;
          }
      }

    public class Solution {
        public boolean hasCycle(ListNode head) {
            Set<ListNode> seen = new HashSet<ListNode>();
            while (head != null) {
                if (!seen.add(head)) {
                    return true;
                }
                head = head.next;
            }
            return false;
        }
    }

}
